package org.hamster.multidropbox.dropbox.vo
{
	import org.hamster.dropbox.DropboxClient;
	import org.hamster.dropbox.DropboxConfig;
	import org.hamster.dropbox.DropboxEvent;
	import org.hamster.dropbox.models.AccountInfo;
	import org.hamster.dropbox.models.DropboxFile;
	import org.hamster.multidropbox.dropbox.constants.AppDropboxClientStatus;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.service.DropboxTreeCacheService;
	import org.hamster.multidropbox.main.vo.IStorableVO;
	
	[Event(type="org.hamster.multidropbox.dropbox.event.AppDropboxEvent", name="clientStatusChanged")]
	public class AppDropboxClient extends DropboxClient implements IStorableVO
	{
		public var accountDetail:AccountInfo;
		private var _status:int = AppDropboxClientStatus.UNKNOWN;
		
		public function set status(value:int):void
		{
			if (_status != value) {
				var oldStatus:int = _status;
				_status = value;
				if (this.hasEventListener(AppDropboxEvent.CLIENT_STATUS_CHANGED)) {
					var disEvt:AppDropboxEvent = new AppDropboxEvent(AppDropboxEvent.CLIENT_STATUS_CHANGED);
					disEvt.oldStatus = oldStatus;
					disEvt.newStatus = value;
					this.dispatchEvent(disEvt);
				}
			}
		}
		
		public function get status():int
		{
			return _status;
		}
		
		public function get uid():int
		{
			if (accountDetail) {
				return accountDetail.uid;
			} else {
				return -1;
			}
		}
		
		public function AppDropboxClient(config:DropboxConfig)
		{
			super(config);
		}
		
		public function encodeXML():XML
		{
			var result:XML = <client request-token-key={config.requestTokenKey} 
									 request-token-secret={config.requestTokenSecret}
			                         access-token-key={config.accessTokenKey}
			                         access-token-secret={config.accessTokenSecret}>
                                 <account-info uid={accountDetail.uid}
			                                   display-name={accountDetail.displayName}
                                               email={accountDetail.email}
											   country={accountDetail.country}
											   referral-link={accountDetail.referral_link}
											   quota-info-normal={accountDetail.quotaInfo_normal}
											   quota-info-quota={accountDetail.quotaInfo_quota}
											   quota-info-shared={accountDetail.quotaInfo_shared}>
                                 </account-info>
                             </client>;
			return result;
		}
		
		public function decodeXML(value:XML):void
		{
			this.config.requestTokenKey    = value.attribute('request-token-key');
			this.config.requestTokenSecret = value.attribute('request-token-secret');
			this.config.accessTokenKey     = value.attribute('access-token-key');
			this.config.accessTokenSecret  = value.attribute('access-token-secret');
			
			var accountDetailXML:XML = value.child('account-info')[0];
			accountDetail = new AccountInfo();
			accountDetail.uid 				= accountDetailXML.attribute('uid');
			accountDetail.displayName 		= accountDetailXML.attribute('display-name');
			accountDetail.email 			= accountDetailXML.attribute('email');
			accountDetail.country 			= accountDetailXML.attribute('country');
			accountDetail.referral_link 	= accountDetailXML.attribute('referral-link');
			accountDetail.quotaInfo_normal 	= accountDetailXML.attribute('quota-info-normal');
			accountDetail.quotaInfo_quota 	= accountDetailXML.attribute('quota-info-quota');
			accountDetail.quotaInfo_shared 	= accountDetailXML.attribute('quota-info-shared');
		}
	}
}